#include "iconmanager.h"

#include "thememanager.h"

#include <QtGui/QApplication>

using namespace core;

IconManager* iconManager = 0;

IconManager* IconManager::getInstance()
{
    if (iconManager == 0)
    {
        iconManager = new IconManager();
    }

    return iconManager;
}

void IconManager::destroy()
{
    delete iconManager;
    iconManager = 0;
}

void IconManager::registerNativeIcon(const QString& name,
    QStyle::StandardPixmap pixmap)
{
    if (!m_nativeIconMapping.contains(name))
    {
        m_nativeIconMapping[name] = pixmap;
    }
}

void IconManager::unregisterNativeIcon(const QString& name)
{
    if (m_nativeIconMapping.contains(name))
    {
        m_nativeIconMapping.remove(name);
    }
}

void IconManager::registerStyleIcon(const QString& name,
    const QString& resource)
{
    if (!m_styleIconMapping.contains(name))
    {
        m_styleIconMapping[name] = resource;
    }
}

void IconManager::unregisterStyleIcon(const QString& name)
{
    if (m_styleIconMapping.contains(name))
    {
        m_styleIconMapping.remove(name);
    }
}

bool IconManager::hasIcon(const QString& name) const
{
    bool ret = ThemeManager::getInstance()->themeApplied() ?
        m_styleIconMapping.contains(name) :
        m_nativeIconMapping.contains(name);

    return ret;
}

QIcon IconManager::icon(const QString& name) const
{
    QIcon ret = ThemeManager::getInstance()->themeApplied() ? styleIcon(name)
        : nativeIcon(name);

    return ret;
}

IconManager::IconManager() : m_nativeIconMapping(), m_styleIconMapping()
{

}

IconManager::~IconManager()
{

}

QIcon IconManager::nativeIcon(const QString& name) const
{
    QIcon ret;

    if (m_nativeIconMapping.contains(name))
    {
        QStyle* style = QApplication::style();
        ret = style->standardIcon(m_nativeIconMapping[name]);
    }
    else
    {
        ret = QIcon(name);
    }

    return ret;
}

QIcon IconManager::styleIcon(const QString& name) const
{
    QIcon ret;

    if (m_styleIconMapping.contains(name))
    {
        ret = QIcon(m_styleIconMapping[name]);
    }
    else
    {
        ret = QIcon(name);
    }

    return ret;
}
